//@author A0094511L
#include "SyntaxHighlighter.h"  
#include <QtGui>

const QString SyntaxHighlighter::PATTERN_KEYWORD = "(\\badd\\b)|(\\bedit\\b)|(\\b(display(?:[w|t|e|d]?))\\b)|(\\bdisplaydb\\b)|(\\bdisplayall\\b)|(\\bdisplaydone\\b)|(\\bhelp date\\b)|(\\bhelp time\\b)|"
												   "(\\bsearch\\b)|(\\bredo\\b)|(\\bundo\\b)|(\\bhelp\\b)|(\\bclear\\b)|(\\bexit\\b)|(\\bdone\\b)|(\\bdelete\\b)|(\\bfrom\\b)|"
												   "(\\bto\\b)|(\\bby\\b)|(\\bat\\b)|(\\bon\\b)|(\\bn\\b)|(\\bsd\\b)|(\\bed\\b)|(\\bst\\b)|(\\bet\\b)";
const QString SyntaxHighlighter::PATTERN_QUOTATIONSMARK = "\".*\"";

 SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent){
	 setColor();
	 setTextCharFormat();
	 defineRegularExpressions();
  
 }
  
void SyntaxHighlighter::setColor(){

	 _normalFormatBrush =  QColor(0,0,0);
	 _highlightedFormatBrush = QColor(43,111,184);
}

 void SyntaxHighlighter::setTextCharFormat(){

	 _normalFormat.setFontWeight(QFont::Normal);
	 _normalFormat.setForeground(_normalFormatBrush); //sets font color

	 _highlightedFormat.setFontWeight(QFont::Bold);
	 _highlightedFormat.setForeground(_highlightedFormatBrush);
 }

 void SyntaxHighlighter::defineRegularExpressions(){
	 
	 _quotationRegex.setPattern(PATTERN_QUOTATIONSMARK);
	 _keyWordRegex.setPattern(PATTERN_KEYWORD);
	 _keyWordRegex.setCaseSensitivity(Qt::CaseInsensitive);
 }

void SyntaxHighlighter::highlightBlock(const QString &textToBeHighlighted){

	highlightCharbyChar(_keyWordRegex, _highlightedFormat, textToBeHighlighted);
	highlightCharbyChar(_quotationRegex, _normalFormat, textToBeHighlighted);
}

void SyntaxHighlighter::highlightCharbyChar(QRegExp regex, QTextCharFormat format, const QString& textToBeHighlighted){
	 
	int index = textToBeHighlighted.indexOf(regex);
	while (index >= 0) {
		int length = regex.matchedLength();
		QSyntaxHighlighter::setFormat(index, length, format);
		index = textToBeHighlighted.indexOf(regex, index + length);
	}
}
  
